package org.hk.proxy.server.web.interceptor;

import cn.hutool.core.util.StrUtil;
import io.netty.handler.codec.http.HttpRequest;
import org.hk.proxy.common.constant.ApiStateEnum;
import org.hk.proxy.common.exception.ApiCommonException;
import org.hk.proxy.server.db.entity.HkProxyUser;
import org.hk.proxy.server.utils.WebUtils;
import org.hk.proxy.server.web.handler.LoginHandler;

import java.util.HashMap;
import java.util.Map;
public class LoginInterceptor implements HttpInterceptor{
    public static final Map<String, HkProxyUser> userMap = new HashMap<>();
    @Override
    public void preRequest(HttpRequest request) {
        String uri = request.uri();
        if(!uri.equalsIgnoreCase("/login")&&!uri.equalsIgnoreCase("/index.html")&&!UriHttpInterceptor.containWebPath(uri)){
            String token = request.headers().get(LoginHandler.AuthHeader);
            if(StrUtil.isBlank(token)||!userMap.containsKey(token)){
                throw new ApiCommonException(ApiStateEnum.NO_LOGIN_IN);
            }
        }
    }
    public LoginInterceptor() {
    }
}
